import bb.cascades 1.2
import bb.cascades.maps 1.2
import bb.cascades.places 1.0
import QtMobility.sensors 1.2
import QtMobilitySubset.location 1.1
import de.heimweghelden 1.0

Page {
    property bool followMe: true
    property int status: 0
    property int duration: undefined
    property int updateGPSInterval: 3000
    onDurationChanged: {
        updateTime();
    }
    onStatusChanged: {
        console.log("STATUS CHANGED", status);
    }
    onFollowMeChanged: {
        console.log("followMe changed", followMe);
    }
    function formatDuration() {
        var durationHours = parseInt(duration/3600, 10);
        var rSeconds = (duration-durationHours*3600);
        var durationMinutes = parseInt(rSeconds/60, 10);
        var durationSeconds = rSeconds-durationMinutes*60;
        var timeString = 
        (durationHours < 10 ? "0" : "") + durationHours + ":" +
        (durationMinutes < 10 ? "0" : "") + durationMinutes + ":" +
        (durationSeconds < 10 ? "0" : "") + durationSeconds;
        return timeString;
    }
    function updateTime() {
        var timeString = formatDuration();
        var value = durationpicker.dateFromTime(timeString);
        durationpicker.setValue(value);
        durationtitlefield.text = timeString;
    }
    titleBar: TitleBar {
        id: titleBarCmp
        visibility: (status == 0 || status == 1 || status == 2) ? ChromeVisibility.Default : ChromeVisibility.Hidden
        kind: TitleBarKind.FreeForm
        kindProperties: FreeFormTitleBarKindProperties {
            id: titleBarProps
            expandableArea.indicatorVisibility: TitleBarExpandableAreaIndicatorVisibility.Hidden
            expandableArea.toggleArea: status == 1 ? TitleBarExpandableAreaToggleArea.EntireTitleBar : TitleBarExpandableAreaToggleArea.IndicatorOnly
            Container {
                topPadding: 15
                rightPadding: 15
                leftPadding: 15
                bottomPadding: 15
                Container {
                    visible: status == 0
                    TextField {
                        verticalAlignment: VerticalAlignment.Fill
                        id: placebox
                        onFocusedChanged: {
                            if (focused) {
                                var place = placepicker.show ()
                                if (place) {
                                    placebox.text = place.name
                                    locationHandler.updateDestinationLocation(place.latitude, place.longitude, place.name);
                                } else {
                                    placebox.text = ""
                                }
                                placebox.loseFocus();
                            }
                        }
                        hintText: qsTr("Where are you going?")
                        maximumLength: 20
                        focusHighlightEnabled: false
                    }
                }
                Container {
                    visible: status == 1
                    layout: StackLayout {
                        orientation: LayoutOrientation.LeftToRight
                    }
                    ImageButton {
                        defaultImageSource: "asset:///images/icons/bb10/ic_previous.png"
                        pressedImageSource: "asset:///images/icons/bb10/ic_previous.png"
                        disabledImageSource: "asset:///images/icons/bb10/ic_previous.png"
                        onClicked: {
                            placebox.text = "";
                            locationHandler.resetDestinationLocation();
                        }
                    }
                    TextField {
                        id: durationtitlefield
                        focusHighlightEnabled: false
                        layoutProperties: StackLayoutProperties {
                            spaceQuota: 1
                        }
                        onFocusedChanged: {
                            if (focused) {
                                durationpicker.expanded = focused;
                                durationtitlefield.loseFocus();
                            }
                        }
                    }
                    ImageButton {
                        defaultImageSource: "asset:///images/icons/bb10/ic_next.png"
                        pressedImageSource: "asset:///images/icons/bb10/ic_next.png"
                        disabledImageSource: "asset:///images/icons/bb10/ic_next.png"
                        visible: (duration != undefined && duration > 0)
                        onClicked: {
                            var sheet = helden.createObject();
                            sheet.open();
                        }
                    }

                }
                Container {
                    horizontalAlignment: HorizontalAlignment.Fill
                    visible: status == 2
                    CountdownTimer {
                        fontSize: 200
                        horizontalAlignment: HorizontalAlignment.Fill
                        verticalAlignment: VerticalAlignment.Fill
                    }
                }
            }
            expandableArea {
                expanded: durationpicker.expanded
                content: Container {
                    visible: status == 1
                    DateTimePicker {
                        id: durationpicker
                        expanded: false
                        minuteInterval: 1
                        mode: DateTimePickerMode.Timer
                        accessibilityMode: A11yMode.Default
                        onValueChanged: {
                            var nseconds = (value.getHours() * 60 + value.getMinutes()) * 60 + value.getSeconds();
                            duration = nseconds;
                            //app.onRouteSetDuration(nseconds);
                        }
                        title: qsTr("Duration")
                        onExpandedChanged: {
                            titleBarProps.expandableArea.expanded = expanded;
                        }
                    }
                }
                onExpandedChanged: {
                    durationpicker.expanded = expanded;
                }
            }
        }
    }
    Container {
        MapView {
            layoutProperties: StackLayoutProperties {
                spaceQuota: 1
            }
            objectName: "mapViewObj"
            horizontalAlignment: HorizontalAlignment.Fill
            verticalAlignment: VerticalAlignment.Fill
            id: mapview
            latitude: 53.5523786
            longitude: 9.9111201

        }
    }
    attachedObjects: [
        Compass {
            active: followMe
            axesOrientationMode: Compass.UserOrientation
            alwaysOn: false
            onReadingChanged: {
                mapview.setHeading(360 - reading.azimuth);
            }
        },
        PositionSource {
            id: positionSource
            updateInterval: updateGPSInterval
            active: followMe
            onPositionChanged: {
                //mapview.latitude = positionSource.position.coordinate.latitude;//TMP
                //mapview.longitude = positionSource.position.coordinate.longitude;//TMP
                // TODO: tell app the current location. the center of the map is possible defined by other sources
                locationHandler.updateDeviceLocation(positionSource.position.coordinate.latitude, positionSource.position.coordinate.longitude);
            }
        },
        PlacePicker {
            id: placepicker
        },
        ComponentDefinition {
            id: helden
            RouteHeldenSheet {
                onStart: {
                    locationHandler.onRouteStart();
                }
            }
        }
    ]
}
